package com.qdxwx.common.utils;


import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public class TKString {
    /**
     * 空字符串
     */
    private static final String NULLSTR = "";
    private static final char SEPARATOR = '_';

    public static String getRanNumber(int len) {
        Random random = new Random();
        StringBuilder sRand = new StringBuilder();
        for (int i = 0; i < len; ++i) {
            sRand.append(random.nextInt(10));
        }
        return sRand.toString();
    }

    public static String getRanString(int len) {
        StringBuilder buffer = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < len; ++i) {
            int charInt;
            char c;
            if (random.nextBoolean()) {
                charInt = 48 + random.nextInt(10);
                c = (char) charInt;
                buffer.append(c);
            } else {
                if (random.nextBoolean()) {
                    charInt = 65 + random.nextInt(26);
                } else {
                    charInt = 97 + random.nextInt(26);
                }

                if (charInt == 79) {
                    charInt = 111;
                }

                c = (char) charInt;
                buffer.append(c);
            }
        }
        return buffer.toString();
    }


    /**
     * 生成随机数字和字母组合
     *
     * @param length [生成随机数的长度]
     * @return 随机数字和字母组合
     */
    public static String getRandom(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (choice + random.nextInt(26)));
            } else { // 数字
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }

    public static boolean validMaxLen(String str, int length) {
        return (str != null && !str.equals("")) && str.length() <= length;
    }

    public static boolean validMinLen(String str, int length) {
        return (str != null && !str.equals("")) && str.length() >= length;
    }

    public static boolean equals(String str1, String str2) {
        return (str1 != null && !str1.equals("") && str2 != null && !str2.equals("")) && str1.equals(str2);
    }

    /**
     * 将字符串转换为数字
     *
     * @param str 要转换字符串
     * @return int
     */
    public static int toInt(String str) {
        return toInt(str, 0);
    }

    /**
     * 将字符串转换为数字
     *
     * @param str 要转换字符串
     * @param def 转换失败默认值
     * @return int
     */
    public static int toInt(String str, int def) {
        return TKConvert.toInteger(str, def);
    }

    /**
     * 将字符串转换为数字
     *
     * @param str 要转换字符串
     * @return long
     */
    public static long toLong(String str) {
        return toLong(str, 0);
    }

    /**
     * 将字符串转换为数字Long
     *
     * @param str 要转换字符串
     * @param def 转换失败默认值
     * @return long
     */
    public static long toLong(String str, long def) {
        return TKConvert.toLong(str, def);
    }

    /**
     * 将字符串转换为数字
     *
     * @param str 要转换字符串
     * @return long
     */
    public static boolean toBoolean(String str) {
        return toBoolean(str, false);
    }

    /**
     * 将字符串转换为数字Long
     *
     * @param str 要转换字符串
     * @param def 转换失败默认值
     * @return long
     */
    public static boolean toBoolean(String str, boolean def) {
        return TKConvert.toBoolean(str, def);
    }

    public static String arrayToString(Object[] array, String split) {
        if (array == null) {
            return "";
        } else {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < array.length; ++i) {
                if (i != array.length - 1) {
                    str.append(array[i].toString()).append(split);
                } else {
                    str.append(array[i].toString());
                }
            }
            return str.toString();
        }
    }

    public static int formatPage(String page) {
        byte iPage = 1;
        if (page != null && !page.equals("")) {
            int iPage1;
            try {
                iPage1 = Integer.parseInt(page);
            } catch (Exception ex) {
                ex.printStackTrace();
                iPage1 = 1;
            }

            return iPage1;
        } else {
            return iPage;
        }
    }

    public static String getFileSize(String fileSize) {
        String temp = "";
        DecimalFormat df = new DecimalFormat("0.00");
        double dbFileSize = Double.parseDouble(fileSize);
        if (dbFileSize >= 1024.0D) {
            if (dbFileSize >= 1048576.0D) {
                if (dbFileSize >= 1.073741824E9D) {
                    temp = df.format(dbFileSize / 1024.0D / 1024.0D / 1024.0D) + " GB";
                } else {
                    temp = df.format(dbFileSize / 1024.0D / 1024.0D) + " MB";
                }
            } else {
                temp = df.format(dbFileSize / 1024.0D) + " KB";
            }
        } else {
            temp = df.format(dbFileSize / 1024.0D) + " KB";
        }

        return temp;
    }

    public static String getEntry() {
        Random random = new Random(100L);
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssS");
        return TKConvert.toMD5(formatter.format(now) + random.nextDouble());
    }

    public static String toUtf8(String str) {
        return toCharset(str, StandardCharsets.UTF_8);
    }

    public static String toCharset(String str, Charset charset) {
        if (str != null && !str.equals("")) {
            try {
                return new String(str.getBytes("ISO8859-1"), charset);
            } catch (Exception ex) {
                ex.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    public static String getChineseNum(int num) {
        String[] chineseNum = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        return chineseNum[num];
    }

    public static String replaceEnter(String str) {
        return str == null ? null : str.replaceAll("\r", "").replaceAll("\n", "");
    }

    public static String toTrim(String str) {
        return null != str ? str.trim() : "";
    }

    public static String createUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String cutffStr(String sourceStr, int length, String charactor) {
        String resultStr = sourceStr;
        if (sourceStr != null && !"".equals(sourceStr)) {
            if (sourceStr.length() > length) {
                resultStr = sourceStr.substring(0, length);
                resultStr = resultStr + charactor;
            }

            return resultStr;
        } else {
            return "";
        }
    }

    public static boolean isNumber(String str) {
        if (str == null || str.trim().equals("")) {
            return false;
        }
        try {
            Long.parseLong(str);
            return true;
        } catch (Exception var3) {
            return false;
        }
    }

    public static String left(String str, int length) {
        if (str == null) {
            return null;
        } else {
            if (str.length() <= length) {
                return str;
            } else {
                return str.substring(0, length) + "...";
            }
        }
    }

    public static String handleEmail(String email) {
        if (email == null) {
            return "";
        } else {
            String[] aryEmail = email.split("@");
            if (aryEmail.length == 2 && aryEmail[0] != null) {
                String firstPart = aryEmail[0].substring(aryEmail[0].length() / 2, aryEmail[0].length());
                if (!"".equals(firstPart)) {
                    char[] repeatChar = new char[firstPart.length()];
                    for (int i = 0; i < firstPart.length(); ++i) {
                        repeatChar[i] = 42;
                    }

                    email = email.replaceFirst(firstPart + "@", new String(repeatChar) + "@");
                }
            }

            return email;
        }
    }

    public static String formatMobile(String mobile) {
        if (mobile == null) return null;
        if (mobile.length() == 11) {
            return mobile.charAt(0) + String.valueOf(mobile.charAt(1)) + mobile.charAt(2) + "****" + mobile.charAt(7) + mobile.charAt(8) + mobile.charAt(9) + mobile.charAt(10);
        } else {
            return mobile;
        }
    }

    public static boolean validNull(String str) {
        return str != null && !str.trim().equals("");
    }

    public static boolean validNull(String... str) {
        for (String s : str) {
            if (s == null || s.trim().equals("")) {
                return false;
            }
        }

        return true;
    }

    public static String getRootPath() {
        return getRootPath("");
    }

    public static String getRootPath(String resource) {
        return getRootPath(Thread.currentThread().getContextClassLoader().getResource(resource));
    }

    public static String getRootPath(URL url) {
        if (url == null) return "/";
        String filePath = url.toString();
        if (filePath.toLowerCase().contains("file:")) {
            filePath = filePath.substring(6);
        }
        if (filePath.toLowerCase().contains("classes")) {
            filePath = filePath.replaceAll("/classes", "");
        }
        if (filePath.toLowerCase().contains("web-inf")) {
            filePath = filePath.substring(0, filePath.length() - 9);
        }
        if (!System.getProperty("os.name").toLowerCase().contains("window")) {
            filePath = "/" + filePath;
        }
        if (!filePath.endsWith("/")) {
            filePath = filePath + "/";
        }
        return filePath;
    }

    public static String getSysTimeRandom() {
        return System.currentTimeMillis() + "" + (new Random()).nextInt(100);
    }

    public static String getSysTimeRandom(int count) {
        String resultRandom = System.currentTimeMillis() + "" + (new Random()).nextInt(100);
        StringBuilder resultRandomPro = new StringBuilder();
        int resultCount = resultRandom.length();
        if (count < resultCount) {
            return resultRandom.substring(resultCount - 1 - count, resultCount - 1);
        } else {
            resultRandomPro.append("0".repeat(count - resultCount));
            return resultRandomPro.append(resultRandom).toString();
        }
    }

    public static String[] sortArray(String[] array) {
        for (int i = 0; i < array.length; ++i) {
            for (int j = 0; j < i; ++j) {
                if (array[i].compareTo(array[j]) < 0) {
                    String temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                }
            }
        }
        return array;
    }

    public static boolean isBlank(String str) {
        return null == str || str.trim().length() == 0;
    }

    /**
     * 去空格
     */
    public static String trim(String str) {
        return (str == null ? "" : str.trim());
    }

    /*
     * 删除开头字符串
     */
    public static String trimStart(String inStr, String prefix) {
        if (inStr.startsWith(prefix)) {
            String ret = inStr.substring(prefix.length());
            if (ret.startsWith(prefix)) {
                ret = trimStart(ret, prefix);
            }
            return ret;
        }
        return inStr;
    }

    /*
     * 删除末尾字符串
     */
    public static String trimEnd(String inStr, String suffix) {
        if (inStr.endsWith(suffix)) {
            String ret = inStr.substring(0, inStr.length() - suffix.length());
            if (ret.endsWith(suffix)) {
                ret = trimStart(ret, suffix);
            }
            return ret;
        }
        return inStr;
    }

    /**
     * 截取字符串
     *
     * @param str   字符串
     * @param start 开始
     * @return 结果
     */
    public static String substring(final String str, int start) {
        if (str == null) {
            return NULLSTR;
        }

        if (start < 0) {
            start = str.length() + start;
        }

        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return NULLSTR;
        }

        return str.substring(start);
    }

    /**
     * 截取字符串
     *
     * @param str   字符串
     * @param start 开始
     * @param end   结束
     * @return 结果
     */
    public static String substring(final String str, int start, int end) {
        if (str == null) {
            return NULLSTR;
        }

        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }

        if (end > str.length()) {
            end = str.length();
        }

        if (start > end) {
            return NULLSTR;
        }

        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }
        return str.substring(start, end);
    }

    /**
     * 下划线转驼峰命名
     */
    public static String toUnderScoreCase(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase;
        // 当前字符是否大写
        boolean curreCharIsUpperCase;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append(SEPARATOR);
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append(SEPARATOR);
            }
            sb.append(Character.toLowerCase(c));
        }
        return sb.toString();
    }

    /**
     * 是否包含字符串
     *
     * @param str  验证字符串
     * @param strs 字符串组
     * @return 包含返回true
     */
    public static boolean inStringIgnoreCase(String str, String... strs) {
        if (str != null && strs != null) {
            for (String s : strs) {
                if (str.equalsIgnoreCase(trim(s))) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串。 例如：HELLO_WORLD->HelloWorld
     *
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    public static String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty()) {
            // 没必要转换
            return "";
        } else if (!name.contains("_")) {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (String camel : camels) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty()) {
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }

    /**
     * 驼峰式命名法 例如：user_name->userName
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    //========================
    public static Timestamp convertToTimestamp(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date myDate;
        Timestamp myTimestamp = null;

        try {
            myDate = sdf.parse(time);
            myTimestamp = new Timestamp(myDate.getTime());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return myTimestamp;
    }

    //清除html标签
    public static String clearHTML(String htmlStr) {
        if (htmlStr == null || htmlStr.length() < 1) return htmlStr;
        Pattern p_html = Pattern.compile("<[^>|^<]*>", Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll("");
        htmlStr = htmlStr.replace("&lt;", "<").replace("&#60;", "<");
        htmlStr = htmlStr.replace("&gt;", ">").replace("&#62;", "<");
        htmlStr = htmlStr.replace("&nbsp;", "").replace("&#160;", "");//空格去掉
        htmlStr = htmlStr.replace("&quot;", "").replace("&#34;", "");//引号去掉
        htmlStr = htmlStr.replace("&amp;", "&").replace("&#38;", "&");//&
        return htmlStr.trim();
    }

    //清理简介
    public static String clearIntro(String str) {
        if (str == null || str.length() < 2) {
            return str;
        }
        str = str.replace("\n", "").replace("\r", "");
        str = str.replace("\t", "").replace("'", "");
        str = str.replace("&ldquo;", "＂").replace("&rdquo;", "＂");
        str = str.replace("&lnbsp;", "").replace("&rnbsp;", "");
        str = str.replace("　", "").replace(" ", "");
        return str.trim();
    }

    /// <summary>
    /// 过滤会导致XML错误的字符
    /// </summary>
    /// <param name="Str"></param>
    /// <returns></returns>
    public static String xmlBadWord(String Str) {
        if (Str == null || Str.equals("")) {
            return Str;
        }
        StringBuilder info = new StringBuilder();
        for (char cc : Str.toCharArray()) {
            if ((int) cc <= 8 || ((int) cc >= 11 && (int) cc <= 12) || ((int) cc >= 14 && (int) cc <= 32))
                info.append(" ");//info.AppendFormat(" ", ss);
            else info.append(cc);
        }
        return info.toString().trim();
    }

    public static String dangerKey(String Str) {
        if (Str == null || Str.equals("")) {
            return Str;
        }
        return Str.replace("'", "").trim();
    }

    //允许@.
    public static String queryKey(String Str) {
        if (Str == null || Str.isEmpty()) return Str;
        String[] TempWord = {
                "`", "~", "!", "#", "$", "%", "^", "&", "*",
                "(", ")", "+", "{", "}", "|", ":", "\"", "<", ">",
                "?", "·", "｀", "～", "！", "＠", "＃", "＄", "％", "＾",
                "＆", "＊", "（", "）", "＿", "＋", "｛", "｝", "｜",
                "：", "＂", "“", "”", "＜", "＞", "？", "=", "[", "]",
                "\\", ";", "'", ",", "/", "－", "＝", "［", "］", "＼",
                "；", "＇", "，", "．", "／", "", "", "", "‘"};
        String KeyStr = Str;
        for (String St : TempWord) {
            if (KeyStr.contains(St)) {
                KeyStr = KeyStr.replace(St, "");
            }
        }
        return KeyStr.trim();
    }

    public static String searchKeyFilter(String str) {
        if (str == null || str.equals("")) {
            return str;
        }
        String regEx = "[`~!@#$%^&*()+=|{}:;\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？']";
        return Pattern.compile(regEx).matcher(str).replaceAll("").trim();
    }

    /**
     * * 判断一个对象数组是否为空
     *
     * @param objects 要判断的对象数组
     *                * @return true：为空 false：非空
     */
    public static boolean isEmpty(Object[] objects) {
        return null == objects || (objects.length == 0);
    }

    public static boolean isEmpty(String str) {
        return null == str || str.trim().length() == 0;
    }

    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isName(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        try {
            return inputStr.matches("^[\\u4e00-\\u9fa5_a-zA-Z0-9-]{1,18}$");
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合email格式
     *
     * @param inputStr email字符串
     * @return false/true
     */
    public static boolean isEmail(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        try {
            return inputStr.matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合密码格式
     *
     * @param inputStr 密码
     * @return false/true
     */
    public static boolean isPassword(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        return (inputStr.length() >= 6 && inputStr.length() <= 20)
                && inputStr.matches("^[_0-9a-zA-Z]{6,}$");
    }

    /**
     * 验证是否符合Url格式
     *
     * @param inputStr Url字符串
     * @return false/true
     */
    public static boolean isURL(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        String regex = "([a-zA-Z0-9\\:\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-\\/]+)*$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean isDomain(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        String regex = "^([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean isDomainURL(String inputStr) {
        if (inputStr == null || inputStr.isEmpty()) return false;
        String regex = "^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合Mobile格式
     *
     * @param inputStr Mobile字符串
     * @return false/true
     */
    public static boolean isMobile(String inputStr) {
        if (inputStr == null) return false;
        try {
            return inputStr.matches("^1[0-9]{10}$");
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合Phone格式
     *
     * @param inputStr Phone字符串
     * @return false/true
     */
    public static boolean isPhone(String inputStr) {
        if (inputStr == null) return false;
        String regex = "(^(\\d{3,4}-?|\\(\\d{3,4}\\))+?(\\d{8}|\\d{7})+?(-\\d{4}|\\(\\d{4}\\))?$)|(^1\\d{10}$)";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合身份证格式
     *
     * @param inputStr 身份证字符串
     * @return false/true
     */
    public static boolean isCard(String inputStr) {
        if (inputStr == null) return false;
        String regex = "(^(\\d{6})(18|19|20)+?(\\d{2})(0+?\\d|1[012])(0+?\\d|[12]\\d|3[01])(\\d{3})(\\d|x|X)+?$)|(^\\d{15}$)";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合English格式
     *
     * @param inputStr English字符串(允许数字)
     * @return false/true
     */
    public static boolean isEnglish(String inputStr) {
        if (inputStr == null) return false;
        String regex = "^[A-Za-z0-9_.]+$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合Ip格式
     *
     * @param inputStr Ip字符串
     * @return false/true
     */
    public static boolean isIP(String inputStr) {
        if (inputStr == null) return false;
        String regex = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证是否符合IPSect格式
     *
     * @param inputStr IPSect字符串
     * @return false/true
     */
    public static boolean isIPSect(String inputStr) {
        if (inputStr == null) return false;
        String regex = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){2}((2[0-4]\\d|25[0-5]|[01]?\\d\\d?|\\*)\\.)(2[0-4]\\d|25[0-5]|[01]?\\d\\d?|\\*)$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    public static boolean isLegitChar(String inputStr) {
        if (inputStr == null) return false;
        String regex = "^[A-Za-z0-9_\u4E00-\u9FA5]+$";
        try {
            return Pattern.compile(regex).matcher(inputStr).matches();
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 验证 order by 语法是否符合规范
     */
    public static boolean isOrderBySql(String value) {
        if (value == null || value.trim().equals("")) {
            return true;
        }
        return value.matches("[a-zA-Z0-9_ ,]+");
    }

    public static boolean inChinese(String inputStr) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(inputStr);
        return m.find();
    }
}

